package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.GroupQueryOrderAdapter
import com.ht.medicaltreatment.bean.QueryOrderBean
import com.ht.medicaltreatment.view.ProjectDialog
import com.ht.medicaltreatment.viewmodel.GroupOrderViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_group_query_order_layout.btnQuery
import kotlinx.android.synthetic.main.activity_group_query_order_layout.etCode
import kotlinx.android.synthetic.main.activity_group_query_order_layout.radioGroup
import kotlinx.android.synthetic.main.activity_group_query_order_layout.recyclerView
import kotlinx.android.synthetic.main.activity_group_query_order_layout.refreshLayout
import kotlinx.android.synthetic.main.activity_group_query_order_layout.tvProject
import kotlinx.android.synthetic.main.include_title_layout.tvTitle

class GroupQueryOrderActivity:BaseActivity(), OnItemClickListener, OnRefreshLoadMoreListener {

    override fun getLayoutId() = R.layout.activity_group_query_order_layout
    private var projectId = ""
    private var currentPage = 1
    private var pageSize = "20"
    private var orderName = ""
    private val model by viewModels<GroupOrderViewModel>()
    private var checkState = 0
    private lateinit var adapter:GroupQueryOrderAdapter

    override fun init() {
        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = GroupQueryOrderAdapter()
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        refreshLayout.setEnableRefresh(true)
        refreshLayout.setOnRefreshLoadMoreListener(this)
        tvTitle.text = "表单查询"

        //选择项目
        tvProject.setOnClickListener{
            var id = -1
            if(projectId.isEmpty()){
                id = -1
            }else{
                id = projectId.toInt()
            }
            ProjectDialog(this,id).show { id, name ->
                tvProject.text = name
                projectId = "$id"
            }
        }

        btnQuery.setOnClickListener {
            showProgress()
            currentPage = 1
            requestData()
        }

        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            if(checkedId == R.id.radio1){
                checkState = 0
            }else if(checkedId == R.id.radio2){
                checkState = 1
            }else{
                checkState = 2
            }
        }
    }

    private fun requestData(){
        orderName = etCode.text.toString()
        model.queryOrder("$currentPage",pageSize,projectId,orderName,"$checkState",{
            dismissProgress()
            if(currentPage > 1){
                adapter.addData(it)
            }else{
                adapter.setNewInstance(it)
            }
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        }, onError = {
            dismissProgress()
            showToast(it)
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as QueryOrderBean.RowsBean
        val intent = Intent(this,GroupOrderListActivity::class.java)
        intent.putExtra("orderId",item.id)
        startActivity(intent)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        currentPage = 1
        requestData()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        currentPage++
        requestData()
    }
}